<?php

namespace Partner\Controller;

use Think\Controller;
use Think\IFilter;
use Think\IReq;
use Think\JSON;
use Think\Isafe;

class SetController extends ComController {

    public function index() {
        $setDB = M('seller_set');
        $set = $setDB->where(array('seller_id' => $this->Seller['seller_id']))->find();
        //dump($set);die;
        $this->assign('set', $set);
        $this->display();
    }

    /*
     * 保存配置信息
     */

    public function edit() {
        $id = IFilter::act(IReq::get('id'), 'int');

        $data = $_POST;
        $data['seller_id'] = $this->Seller['seller_id'];

        $setDB = M('seller_set');
        //dump($_POST);die;
        if ($id) {
            $setDB->save($data);
        } else {
            $setDB->add($data);
        }
        //$res = dump($setDB->getLastSql());die;
        $this->redirect('set/index');
    }

    /*
     * 企业信息
     */

    public function info() {
        $seller_id = $this->Seller['seller_id'];
        $sellerObj = M('seller');
        $sellerRow = $sellerObj->where(array('id' => $seller_id))->find();
        //dump($sellerRow);die;
        $this->assign('sellerRow', $sellerRow);
        $this->assign('json_sellerRow', JSON::encode($sellerRow));
        $this->display();
    }

    /**
     * 保存企业信息 
     */
    public function info_update() {
        $seller_id = IFilter::act(IReq::get('id'), 'int');
        $seller_name = IFilter::act(IReq::get('seller_name'));
        $email = IFilter::act(IReq::get('email'));
        $password = IFilter::act(IReq::get('password'));
        $repassword = IFilter::act(IReq::get('repassword'));
        $truename = IFilter::act(IReq::get('true_name'));
        $phone = IFilter::act(IReq::get('phone'));
        $mobile = IFilter::act(IReq::get('mobile'));
        $province = IFilter::act(IReq::get('province'), 'int');
        $city = IFilter::act(IReq::get('city'), 'int');
        $area = IFilter::act(IReq::get('area'), 'int');
        $cash = IFilter::act(IReq::get('cash'), 'float');
        $is_vip = IFilter::act(IReq::get('is_vip'), 'int');
        $is_lock = IFilter::act(IReq::get('is_lock'), 'int');
        $address = IFilter::act(IReq::get('address'));
        $account = IFilter::act(IReq::get('account'));
        $server_num = IFilter::act(IReq::get('server_num'));
        $home_url = IFilter::act(IReq::get('home_url'));
        $sort = IFilter::act(IReq::get('sort'), 'int');
        $name = IFilter::act(IReq::get('name'));

        if (!$seller_id && $password == '') {
            $errorMsg = '请输入密码！';
        }

        if ($password != $repassword) {
            $errorMsg = '两次输入的密码不一致！';
        }

        //创建商家操作类
        $sellerDB = M("seller");

        if ($sellerDB->where(array('seller_name' => $seller_name, "id != {$seller_id}"))->find()) {
            $errorMsg = "登录用户名重复";
        } else if ($sellerDB->where(array('true_name' => $truename, "id != {$seller_id}"))->find()) {
            $errorMsg = "商户真实全程重复";
        }

        //操作失败表单回填
        if (isset($errorMsg)) {
            $this->sellerRow = $_POST;
            $this->redirect('seller_edit', false);
            Util::showMessage($errorMsg);
        }

        //待更新的数据
        $sellerRow = array(
            'true_name' => $truename,
            'account' => $account,
            'phone' => $phone,
            'mobile' => $mobile,
            'email' => $email,
            'address' => $address,
            'is_vip' => $is_vip,
            'is_lock' => $is_lock,
            'cash' => $cash,
            'province' => $province,
            'city' => $city,
            'area' => $area,
            'server_num' => $server_num,
            'home_url' => $home_url,
            'sort' => $sort,
            'name' => $name
        );

        //商户资质上传
        if (isset($_FILES['paper_img']['name']) && $_FILES['paper_img']['name']) {
            $uploadObj = new PhotoUpload();
            $uploadObj->setIterance(false);
            $photoInfo = $uploadObj->run();
            if (isset($photoInfo['paper_img']['img']) && file_exists($photoInfo['paper_img']['img'])) {
                $sellerRow['paper_img'] = $photoInfo['paper_img']['img'];
            }
        }


        //修改密码
        if ($password) {
            $sellerRow['password'] = md5($password);
        }

        $sellerRow['id'] = $seller_id;
        $res = $sellerDB->save($sellerRow);

        if ($res) {
            $this->success('编辑成功');
        } else {
            $this->error('编辑失败');
        }
    }

    //微信配置
    function config() {
        $data = M('seller_wx')->where(array('seller_id' => $this->Seller['seller_id']))->find();
        $this->assign('data', $data);
        $this->display();
    }

    function config_act() {
        $id = IFilter::act(IReq::get('id'), 'int');
        //dump($_FILES);die;
        $data = $_POST;
        if ($_FILES) {
            $dir = 'Public/Upload/cert/' . $this->Seller['seller_id'];
            $Upload = new \Think\IUpload();
            $Upload->isForge = $isForge;
            $Upload->setDir($dir);
            $upState = $Upload->execute();
            foreach ($upState as $key => $value) {
                if ($value[0]['flag'] == 1) {
                    $data[$key] = './' . $value[0]['fileSrc'];
                }
            }
        }

        $data['seller_id'] = $this->Seller['seller_id'];

        $setDB = M('seller_wx');
        //dump($data);die;
        if ($id) {
            $setDB->save($data);
        } else {
            $setDB->add($data);
        }
        //$res = dump($setDB->getLastSql());die;
        $this->redirect('set/config');
    }

    //菜单管理
    function menu() {
        $menuObj = M('menu');
        if ($_POST) {
            if (ini_get("magic_quotes_gpc") == "1") {
                $_POST['content'] = stripslashes($_POST['content']);
            }
            $post_data = JSON::decode($_POST['content']);
            $data = array(
                //'id' => $post_data['PModules'][0]['id'],
                'type' => $post_data['PModules'][0]['type'],
                'sort' => $post_data['PModules'][0]['sort'],
                'showInhome' => $post_data['PModules'][0]['content']['showInhome'],
                'showInuser' => $post_data['PModules'][0]['content']['showInuser'],
                'showIncustom' => $post_data['PModules'][0]['content']['showIncustom'],
                'showIngoodslist' => $post_data['PModules'][0]['content']['showIngoodslist'],
                'curMainindex' => $post_data['PModules'][0]['content']['curMainindex'],
                'curSubindex' => $post_data['PModules'][0]['content']['curSubindex'],
                'style' => $post_data['PModules'][0]['content']['style'],
                'dataset' => JSON::encode($post_data['PModules'][0]['content']['dataset']),
                'seller_id' => $this->Seller['seller_id']
            );
            if ($post_data['PModules'][0]['id'] == 1) {
                $id = $menuObj->add($data);
            } else {
                $data['id'] = $post_data['PModules'][0]['id'];
                $id = $menuObj->save($data);
                $id = $id ? $post_data['PModules'][0]['id'] : null;
            }

            if ($id) {
                $res = array(
                    'status' => 1
                );
            } else {
                $res = array(
                    'status' => 0,
                    'msg' => '保存失败'
                );
            }
            
            echo JSON::encode($res); die;
        }

        $seller_id = $this->Seller['seller_id'];
        $where = array('seller_id' => $seller_id);
        $menu = $menuObj->where($where)->find();
        if($menu){
        $menu_data = array(
            'PModules' => array(
                array(
                'id' => $menu['id'],
                'type' => $menu['type'],
                'sort' => $menu['sort'],
                'content' => array(
                    'showInhome' => $menu['showinhome'],
                    'showInuser' => $menu['showinuser'],
                    'showIncustom' => $menu['showincustom'],
                    'showIngoodslist' => $menu['showingoodslist'],
                    'curMainindex' => $menu['curmainindex'],
                    'curSubindex' => $menu['cursubindex'],
                    'style' => $menu['style'],
                    'dataset' => JSON::decode($menu['dataset'])
                ))
            ),
            'LModules' => array()
        );
        //dump($menu);dump($menu_data['PModules'][0]['content']);
        //die;
        //dump($menu_data['PModules'][0]['content']);
        //$menu_data = '{"PModules":[{"id":1,"type":"Navigation","draggable":false,"sort":0,"content":{"showInhome":true,"showInuser":true,"showIncustom":true,"showIngoodslist":true,"curMainindex":-1,"curSubindex":-1,"style":"0","dataset":[{"linkType":9,"link":"#","title":"","showtitle":"申请分销","icon":"/PublicMob/images/icon/style1/color0/icon_fx.png","bgColor":"#fff","fotColor":"#333","subNav":[{"linkType":8,"link":"/User/dist_apply","title":"分销申请","showtitle":"分销申请"}]},{"linkType":7,"link":"/User/index","title":"会员主页","showtitle":"会员主页","icon":"/PublicMob/images/icon/style1/color0/icon_user.png","bgColor":"#fff","fotColor":"#333","subNav":[{"linkType":10,"link":" /Item/lists","title":"全部商品","showtitle":"全部商品"}]}],"modulePadding":5},"dom_conitem":null,"ue":null,"dom_ctrl":null}]}';
        //$aa = JSON::decode($menu_data);
        //dump($aa['PModules'][0]['content']);die;
        $this->assign('menu', JSON::encode($menu_data));
        }
        $this->display();
    }

    public function repass() {
        $seller_id = $this->Seller['seller_id'];

        $password = IFilter::act(IReq::get('password'));
        $password1 = IFilter::act(IReq::get('password1'));
        $repassword = IFilter::act(IReq::get('repassword'));
        if ($password) {
            if ($password1 != $repassword) {
                $this->error('两次密码不一致');
            }
            $sellerDB = M('seller');
            $seller = $sellerDB->where(array('id' => $seller_id))->find();
            if (md5($password) == $seller['password']) {
                $sellerRow = array(
                    'password' => md5($password1),
                    'id' => $seller_id
                );
                ISafe::set('seller_pwd', $sellerRow['password']);
                $sellerDB->save($sellerRow);
                redirect(U('Login/index' ,'' ,'') ,2 ,'修改成功！');
            } else {
                $this->error('原密码不正确');
            }
        }

        $this->display();
    }

}